package ex.com.jm.food.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;


import ex.com.jm.food.dao.AdminMenuDao;
import ex.com.jm.food.entity.Menu;
import ex.com.jm.food.entity.SubType;
/**
 * 
 * @author zhaojw
 *
 */
public class AdminMenuDaoImpl extends HibernateDaoSupport implements
		AdminMenuDao {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<Menu> getMenus(final int initPage,final int pageRow,final String name,
			final Integer type,final Integer status,final Integer subId,final Integer supId,final String s_date,final String e_date,final String sort,final String order) {
		// TODO Auto-generated method stub
		Object menus=this.getHibernateTemplate().execute(new HibernateCallback() {
	// [菜单名称][上传企业][所属大类][所属小类][类型][状态][上传时间][上架时间][现价][销售数量]

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="SELECT new ex.com.jm.food.entity.dto.MenuDTO(menu.menuId,"
						+ "menu.menuName,"
						+ "com.comName,"
						+ "menu.status,"
						+ "CASE WHEN menu.menuType=0 THEN '普通菜' WHEN menu.menuType=1 THEN '特价菜' WHEN menu.menuType=2 THEN '热销菜' WHEN menu.menuType=3 THEN '新菜品' END,"
						+ "menu.menuDate,"
						+ "menu.menuInDate,"
						+ "menu.menuNowPrice) "
						+ " FROM Menu AS menu"
						+ " LEFT JOIN menu.comId com "
						+ " WHERE 1=1 ";
				if(name!=null&&!name.equals("")){
					hql+=" AND menu.menuName LIKE '%"+name+"%'";
				}
				if(type!=null&&!type.equals("")){
					hql+=" AND menu.menuType="+type;
				}
				if(status!=null&&!status.equals("")){
					hql+=" AND menu.status="+status;
				}
				if(s_date!=null&&!s_date.equals("")){
					hql+=" AND menu.menuDate>='"+s_date+"'";
				}
				if(e_date!=null&&!e_date.equals("")){
					hql += " AND menu.menuDate<='"+e_date+"'";
				}
				if(sort!=null&&!sort.equals("")&&order!=null&&!order.equals("")){
					hql +=" ORDER BY "+order+" "+sort;
				}else {
					hql+=" ORDER BY menu.menuDate DESC";
				}
				Query query=session.createQuery(hql).setFirstResult(initPage).setMaxResults(pageRow);
				return query.list();
			}
		});
		return (List<Menu>) menus;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public int menuCount(final String name,final Integer type,final Integer status,final Integer subId,final Integer supId,final String s_date,final String e_date) {
		// TODO Auto-generated method stub
		int count=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="from Menu menu where 1=1 ";
				if(name!=null&&!name.equals("")){
					hql+=" AND menu.menuName LIKE '%"+name+"%'";
				}
				if(type!=null&&!type.equals("")){
					hql+=" AND menu.menuType="+type;
				}
				if(status!=null&&!status.equals("")){
					hql+=" AND menu.status="+status;
				}
				if(s_date!=null&&!s_date.equals("")){
					hql+=" AND menu.menuDate>='"+s_date+"'";
				}
				if(e_date!=null&&!e_date.equals("")){
					hql += " AND menu.menuDate<='"+e_date+"'";
				}
				Query query=session.createQuery(hql);
				return query.list().size();
			}
		});
		System.out.println(count);
		return count;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public boolean updateTypea(final String id,final int type) {
		// TODO Auto-generated method stub
		int count=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub 
				String hql="update Menu set menuType="+type+" where menuId='"+id+"'";
				Query query=session.createQuery(hql);
				return query.executeUpdate();
			}
		});
		if (count==1) {
			return true;
		}
		return false;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public boolean updateStatus(final String id,final int status) {
		// TODO Auto-generated method stub
		int count=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub 
				String hql="update Menu set status="+status+" where menuId='"+id+"'";
				Query query=session.createQuery(hql);
				return query.executeUpdate();
			}
		});
		if (count==1) {
			return true;
		}
		return false;
	}

	@Override
	public Object saveMenu(Menu menu) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().save(menu);
	}

	@Override
	public void update(Menu menu) {
		// TODO Auto-generated method stub
         this.getHibernateTemplate().update(menu);
	}

	@Override
	public Menu editMenu(String id) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().get(Menu.class, id);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<SubType> getSubTypes() {
		// TODO Auto-generated method stub
		Object subTypes=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Query query=session.createQuery("from SubType");
				return query.list();
			}
		});
		return (List<SubType>) subTypes;
	}

}
